package com.j.lemon.learn.asiainfo;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @Author lijunjun
 * @Date 2020-06-23 11:14
 * @Description
 */
public class SubStr {
    public static void sub(String startMark,String endMark,String content){
        String[] arrStr;
        if(StringUtils.isNotBlank(startMark) && StringUtils.isNotBlank(endMark)){
            arrStr = StringUtils.substringsBetween(content, startMark, endMark);
            for (int i = 0; i < arrStr.length; i++) {
                arrStr[i] = startMark+arrStr[i]+endMark;
            }

        }else{
            content = content.replaceAll("\n|\r","");
            if(StringUtils.isNotBlank(startMark)){
                arrStr = StringUtils.splitByWholeSeparator(content,startMark);
                for (int i = 0; i < arrStr.length; i++) {
                    arrStr[i] = startMark+arrStr[i];
                }
            }else{
                arrStr = StringUtils.splitByWholeSeparator(content,endMark);
                for (int i = 0; i < arrStr.length; i++) {
                    arrStr[i] = arrStr[i]+endMark;
                }
            }
        }

        System.out.println(Arrays.asList(arrStr).toString());

    }


    public static List<String> sub2(String startMark, String endMark, String content){
        List<String> messages = new ArrayList<>();
        int positionIndex = 0;
        int tagCount = 1;
        int subStart;
        int subEnd;
        //去掉换行符
        content = content.replaceAll("[\n\r]","");
        while(true){
            //开始tag和结束tag都有
            if(StringUtils.isNotBlank(startMark) && StringUtils.isNotBlank(endMark)){
                //收到的文本不包含开始tag或者结束tag，无法截取，不处理，直接去解析
                if(!StringUtils.contains(content,startMark) || !StringUtils.contains(content,endMark)){
                    messages.add(content);
                    break;
                }
                //循环停止条件
                if(positionIndex+startMark.length() > content.length()){
                    messages.add(content.substring(positionIndex));
                    break;
                }
                subStart = content.indexOf(startMark,positionIndex);
                subEnd = content.indexOf(endMark,positionIndex)+endMark.length();
                String singleLog = content.substring(subStart,subEnd);
                positionIndex+=singleLog.length();
                messages.add(singleLog);
            }else{
                //只有开始tag
                if(StringUtils.isNotBlank(startMark)){
                    //收到的文本不包含开始tag，无法截取，不处理，直接去解析
                    if(!StringUtils.contains(content,startMark)){
                        messages.add(content);
                        break;
                    }
                    subStart = StringUtils.ordinalIndexOf(content,startMark,tagCount);
                    //循环结束条件
                    if(-1 == subStart){
                        break;
                    }
                    tagCount++;
                    subEnd = StringUtils.ordinalIndexOf(content,startMark,tagCount)==-1? content.length():StringUtils.ordinalIndexOf(content,startMark,tagCount);
                    messages.add(content.substring(subStart,subEnd));
                //只有结束tag
                }else{
                    //收到的文本不包含结束tag，无法截取，不处理，直接去解析
                    if(!StringUtils.contains(content,endMark)){
                        messages.add(content);
                        break;
                    }
                    subStart=positionIndex;
                    //循环结束条件
                    if(-1 == StringUtils.ordinalIndexOf(content,endMark,tagCount)){
                        break;
                    }
                    subEnd = StringUtils.ordinalIndexOf(content,endMark,tagCount)+endMark.length();
                    tagCount++;
                    positionIndex=subEnd;
                    messages.add(content.substring(subStart,subEnd));
                }
            }
        }
        return messages;
    }

    public static String genSeqId(){

        long curTime = System.currentTimeMillis();
        String time = String.valueOf(curTime);
        if(time.length()>13){
            time = time.substring(time.length()-13);
        }

        String uuid = MD5Util.encrypt16(UUID.randomUUID().toString()).toUpperCase();

        String random = String.valueOf(curTime%100);
        if(random.length()<2)
            random = "0" + random;

        return time + "A" + uuid + random;

    }



    public static void main(String[] args) {
//        String str = "<85>charGate:optTime=20200611150731,sessionId=489508782-721519661620257,sessionIdPa=hXlBJXJRJ7mkGrvndvBqDFylB2nSb1WqzTtp2y1vqGH4cPXgTxR0!-1170965339!1410847889067,pracct=dingyi,slacct=foura,srcIP=192.168.62.113,srcMAC=00:24:E8:AE:28:B6,srcHost=PC-DING,dstIP=192.168.32.204,dstPort=22,dstName=db204,dstId=1791,dstHost=host204,oper=ls,operProtocol=SSH,operDir=/home/foura,result=0,despCmd=ls t,extJson=JTdCJTIyb3BlclJlc3AlMjIlM0ElMjJ0MSUyMHQyJTIyJTdE,errMsg=,workOrder=,workOrderType=,fromSys=UGATE\n";
//        String content = "<B>charGate:optTime=20200611150731<B>1";
//        StopWatch stopWatch = new StopWatch();
//        stopWatch.start();
//        List<String> strings = sub2("<B>", "", content);
//        stopWatch.stop();
//        System.out.println(stopWatch.getTotalTimeMillis());
//        System.out.println(strings);
//
//
//        System.out.println(Integer.toBinaryString(85));
//
//        System.out.println(Integer.valueOf("01010",2));
//        System.out.println(Integer.valueOf("101",2));
        Map<String,Boolean> map = new HashMap<>();

        StringBuilder stringBuilder = new StringBuilder();

        long start=System.currentTimeMillis();
        for(int i=0; i<1000; i++) {
            stringBuilder.append(genSeqId() + ",");
        }

        long end = System.currentTimeMillis()-start;

        System.out.println("耗时:" + end);

        String[] seqIds = stringBuilder.toString().split(",");

        for(String item: seqIds){
            if(map.containsKey(item)){
                System.out.println("contain seqid:"+item);
            }else{
                map.put(item, Boolean.TRUE);
            }
        }

        System.out.println("map size:" + map.keySet().size());

    }
}
